import {loadCascaderDic, loadDic, loadLocalDic, sendDic} from '../../avue/core/dic';
import {DIC_PROPS, KEY_COMPONENT_NAME} from '../../avue/global/variable';
import slot from '../../avue/core/slot'
import {b} from "components/avue/utils/bem";
import {h, resolveComponent} from "vue";

export function getComponentName(column) {
    if (column.component) {
        return column.component;
    }
    let component = column.type || "input";
    if (component.startsWith(KEY_COMPONENT_NAME)) {
        return component;
    }
    return KEY_COMPONENT_NAME + component;

}

export default {
    mixins: [slot],
    emits: ['update:modelValue', 'update:defaults', 'change'],
    props: {
        defaults: {
            type: Object,
            default() {
                return {};
            }
        },
        option: {
            type: Object,
            required: true,
            default: () => {
                return {};
            }
        }
    },
    computed: {
        isMobile() {
            return document.body.clientWidth <= 768;
        },
        resultOption() {
            return {
                ...this.tableOption,
                ...{
                    column: this.propOption || []
                }
            }
        },
        rowKey() {
            return this.tableOption.rowKey || DIC_PROPS.rowKey;
        },
        formRules() {
            return this.propOption.reduce((result, ele) => {
                if (ele.rules && ele.display !== false) {
                    result[ele.prop] = ele.rules;
                }
                return result;
            }, {});
        }
    },
    watch: {
        defaults: {
            handler(val) {
                this.objectOption = val;
            },
            deep: true
        },
        objectOption: {
            handler(val) {
                this.$emit('update:defaults', val)
            },
            deep: true
        },
        propOption: {
            handler(list) {
                this.objectOption = {};
                list.forEach(ele => this.objectOption[ele.prop] = ele);
            },
            deep: true,
        },
        option: {
            handler() {
                this.init(false);
            },
            deep: true,
        },
    },
    data() {
        return {
            DIC: {},
            cascaderDIC: {},
            tableOption: {},
            objectOption: {}
        };
    },
    mounted() {
        this.init();
    },

    methods: {
        b(el, mods) {
            return b(this, el, mods);
        },
        init(type) {
            let tableOption = this.deepClone(this.option)
            let columns = tableOption.column
            debugger
            for (const column of columns) {
                if (column.cell === true) {
                    continue
                }
                let componentName = getComponentName(column)
                const component = resolveComponent(componentName);
                const props = {...column}
                column.dataKey=column.prop
                column.title=column.label
                column.cellRenderer = (scope) => {
                    return h(component, props)
                }

            }

            console.log("tableOption", tableOption)
            this.tableOption = tableOption;
            this.handleLocalDic();
            if (type !== false) {
                this.handleLoadDic()
            }
        },
        dicInit(type) {
            if (type === 'cascader') {
                this.handleLoadCascaderDic()
            } else {
                this.handleLoadDic();
            }
        },
        updateDic(prop, list) {
            let column = this.findObject(this.propOption, prop);
            if (this.validatenull(list) && this.validatenull(prop)) {
                this.handleLoadDic();
            } else if (this.validatenull(list) && !this.validatenull(column.dicUrl)) {
                sendDic({column}).then(list => {
                    this.DIC[prop] = list;
                });
            } else {
                this.DIC[prop] = list;
            }
        },
        handleSetDic(list, res = {}) {
            Object.keys(res).forEach(ele => {
                list[ele] = res[ele]
            });
        },
        //本地字典
        handleLocalDic() {
            loadLocalDic(this.resultOption, this)
        },
        // 网络字典加载
        handleLoadDic() {
            loadDic(this.resultOption, this)
        },
        //级联字典加载
        handleLoadCascaderDic() {
            loadCascaderDic(this.propOption, this)
        }
    }

}
